﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace devtm.Aop
{

    [System.Diagnostics.DebuggerStepThrough]
    public static class RequireHelper
    {


        public static void NotNull(this object value, string name)
        {

            string str = value as string;                
            if (str != null && String.IsNullOrEmpty(str))
                throw new ArgumentNullException(name);
            if (value == null) throw new ArgumentNullException(name);
        }


        public static void Overridable(this MethodInfo m)
        {
            if (!m.IsVirtual || m.IsFinal)
                throw new InvalidOperationException(string.Concat("member must be virtual and not final: ", m.DeclaringType.FullName, ".", m.Name));
        }


        public static void EnsureIsNotInterface(this Type type, string name)
        {
            if (type.IsInterface)
                throw new ArgumentException("Can't be interface", name);
        }


        public static void EnsureIsInterface(this Type type, string name)
        {
            if (!type.IsInterface)
                throw new ArgumentException("must be interface", name);
        }


        public static void EnsureIsNotSealed(this Type type, string name)
        {
            if ((type.Attributes & System.Reflection.TypeAttributes.Sealed) == System.Reflection.TypeAttributes.Sealed)
                throw new ArgumentException("the type Can't be sealed", name);
        }

        public static void EnsureImplement(this Type type, Type[] Interfaces)
        {

            var m = type.GetInterfaces();

            var l = Interfaces.Intersect(m).Count();

            if (l != Interfaces.Count())
                throw new ArgumentException("the type not implement all the interfaces");

        }

    }

}
